// pages/edit/index.js
const { merchantDetail } =  require('../../http/api')
const { merchantPage } =  require('../../http/api')
const { cate } =  require('../../http/api')
const {
  baseUrl
} = require('../../http/env').prod
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    name:'',
    accounts:'',
    accountIndex:1,
    tempFilePaths:'',
    tempFilePathsCopy:'',
    IDcardPaths:'',
    IDcardPathsCopy:'',
    phone:'',
    agentNo:'',
    // reason:'',
    url1:'', //营业执照
    url4:'', //营业执照反面
    url2:'', //身份证正面
    url3:'', //身份证反面
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.merchantDetail()
    this.getcate()
  },
  merchantDetail(){
    var _this = this
    const id = {
      id:this.data.id
    }
    merchantDetail(id).then(res=>{
      if(res.code==200){
        _this.setData({
          name:res.data.name,
          accountIndex:res.data.cate_id,
          tempFilePaths:baseUrl+res.data.zhizhao,
          tempFilePathsCopy:baseUrl+res.data.zhizhao_back,
          IDcardPaths:baseUrl+res.data.card_front,
          IDcardPathsCopy:baseUrl+res.data.card_back,
          phone:res.data.mobile,
          agentNo:res.data.re_code,
          reason:res.data.reason,
          url1:res.data.zhizhao, //营业执照
          url2:res.data.card_front, //身份证正面
          url3:res.data.card_back,
          url4:res.data.zhizhao_back
        })
      }
    })
  },
  getcate(){
    cate().then(res=>{
      const list = []
      res.data.forEach((e)=>{
        list.push(e.name)
      })
      this.setData({
        accounts:list
      })
    })
  },
   // 上传正本
   uploadFront(v){
    const index = v.currentTarget.dataset.index  
    var _this = this;
    if(index == 0){
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          _this.setData({
            tempFilePaths:res.tempFilePaths
          })
          const url = _this.data.tempFilePaths
          console.log(url)
          wx.setStorage({
            key:"key",
            data:url
          })
          wx.uploadFile({
            url:baseUrl + '/api/index/upload', 
            filePath: url[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: res=>{
              _this.setData({
                tempFilePath:res.data
              })
            },
          })
        }
      })
    }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          _this.setData({
            tempFilePathsCopy:res.tempFilePaths
          })
          const url = _this.data.tempFilePathsCopy
          console.log(url)
          wx.setStorage({
            key:"key",
            data:url
          })
          wx.uploadFile({
            url:baseUrl + '/api/index/upload', 
            filePath: url[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: res=>{
              console.log(res.data,999)
              _this.setData({
                url4:res.data,
              })
            },
          })
        }
      })
    }
   
    
  },
  // 身份证
  uploadIDcard(v){
    const index = v.currentTarget.dataset.index
    var _this = this;
    if(index == 0){
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          _this.setData({
            IDcardPaths:res.tempFilePaths
          })
          const url = _this.data.IDcardPaths
          wx.setStorage({
            key:"key1",
            data:url
          })
          wx.uploadFile({
            url:baseUrl + '/api/index/upload', 
            filePath: url[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: res=>{
              _this.setData({
                IDcardPath:res.data
              })
              console.log(_this.data.IDcardPath)
            },
          })
        }
      })
    }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          _this.setData({
            IDcardPathsCopy:res.tempFilePaths
          })
          const url = _this.data.IDcardPathsCopy
          wx.setStorage({
            key:"key2",
            data:url
          })
          wx.uploadFile({
            url:baseUrl + '/api/index/upload', 
            filePath: url[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: res=>{
              _this.setData({
                IDcardPathsCopys:res.data
              })
              console.log(_this.data.IDcardPathsCopys)
            },
          })
        }
      })
    }
  },
  bindAccountChange: function(e) {
    const value = e.detail.value
    console.log(value)
    this.setData({
        accountIndex: value,
    })
  },
 // 提交按钮
 btnSubmit(){
   console.log(this.data.url4)
   if(this.data.url4==null){
    wx.showToast({
      title: '请上传营业执照副本',
      icon:'none',
      duration:1500
    })
    return
  }
  var obj = {
    name:this.data.name,
    cate_id:this.data.accountIndex,
    zhizhao:this.data.url1,
    zhizhao_back:this.data.url4,
    card_front:this.data.url2,
    card_back:this.data.url3,
    mobile:this.data.phone,
    re_code:this.data.agentNo,
    id:this.data.id
  }
  console.log(obj)
  merchantPage(obj).then(res=>{
    if(res.code == 200){
      this.setData({
        id:res.id
      })
    }
    let uid =res.id
    wx.navigateTo({
      url: '../signature/index?id=' +uid,
    })
  })
  this.setData({
    reject:this.reject,
    showOneButtonDialog:true,
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})